﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Data;
using System.Runtime.Remoting.Messaging;



namespace Red_Cross_Philippines
{
    public struct Blood_Client_Donors
    {
        public string Age, Name, bloodType, Sex, date, birthdate, contact, address;
        public byte[] profilepic;
    }

    public struct Account_Registration
    {
        public string name, email, username, password, position;
    }
    public struct Change_Password
    {
        public string passwordUp;
    }

    public class Blood_Bank_System
    {
        OdbcConnection oc = new OdbcConnection();
        OdbcDataAdapter oda = new OdbcDataAdapter();
        public DataTable tbl = new DataTable();


        private void ConnectSQL()
        {
            try
            {
                oc.ConnectionString = "Driver={MySQL ODBC 5.1 Driver};" +
                                          "Server=localhost;" +
                                          "Database=bloodbank;" +
                                          "UID=Michael;" +
                                           "PWD=michael30";
            }
            catch (Exception) { }

        }

        //Async for Connect Method
        public delegate void ConnectMethod();
        public void Connect()
        {
            ConnectMethod InvokeThis = new ConnectMethod(ConnectSQL);
            InvokeThis.BeginInvoke(null, null);
            System.Threading.Thread.Sleep(10);

        }

        private void DisconnectSQL()
        {
            oc.Close();
        }

        //Async for Disonnect Method
        public delegate void DisconnectMethod();
        public void Disconnect()
        {
            DisconnectMethod Invokethis = new DisconnectMethod(DisconnectSQL);
            Invokethis.BeginInvoke(null, null);
            System.Threading.Thread.Sleep(10);
        }


       public void Query(string sql)
        {

            try
            {
                tbl = new DataTable();
                oda.SelectCommand = new OdbcCommand(sql, oc);
                tbl.Clear();
                tbl.Reset();
                oda.Fill(tbl);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.ToString());
            }
        }

       //Async for Query Method
        public delegate void QueryMethod(string sql);
        public void QuerySQL(string sql)
        {
            QueryMethod InvokeThis = new QueryMethod(QuerySQL);
            InvokeThis.BeginInvoke(sql, null, null);
            System.Threading.Thread.Sleep(100);
        }


     

        private void UpdateTable()
        {
            OdbcCommandBuilder builder = new OdbcCommandBuilder(oda);
            oda.Update(tbl);
        }

        public void Register_Blood_Donors(Blood_Client_Donors clients)
        {
            try
            {
                DataRow row = tbl.NewRow();

                row[1] = clients.date;
                row[2] = clients.Name;
                row[3] = clients.bloodType;
                row[4] = clients.Age;
                row[5] = clients.Sex;
                row[6] = clients.birthdate;
                row[7] = clients.address;
                row[8] = clients.contact;
                row[9] = clients.profilepic;
                tbl.Rows.Add(row);
                UpdateTable();

            }
            catch (Exception) { }

        }

        public void Register_Accnt(Account_Registration account)
        {
            try
            {
                DataRow row = tbl.NewRow();
                row[1] = account.name;
                row[2] = account.email;
                row[3] = account.username;
                row[4] = account.password;
                row[5] = account.position;
                tbl.Rows.Add(row);
                UpdateTable();
            }
            catch (Exception) { }
        }

        public void editAccount_Record(Account_Registration accn, int i)
        {
            try
            {
                tbl.Rows[i][1] = accn.name;
                tbl.Rows[i][2] = accn.email;
                tbl.Rows[i][3] = accn.username;
                tbl.Rows[i][4] = accn.password;
                tbl.Rows[i][5] = accn.position;
                UpdateTable();
            }
            catch (Exception) { }
            return;
        }

        public void updatePassword(Change_Password chPass, int i)
        {
            try
            {
                tbl.Rows[i][4] = chPass.passwordUp;
                UpdateTable();
            }
            catch (Exception) { }
            
        }

        public void View_Blood_Donors()
        {
            try
            {
                tbl = new DataTable();
                oda.SelectCommand = new OdbcCommand("SELECT * FROM tblblooddonor", oc);
                tbl.Clear();
                tbl.Reset();
                oda.Fill(tbl);
            }
            catch (Exception) { }
        }

    }
}